﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using WF.Common;
using Com.DAO;

namespace WF.Web.Components.WorkSpace
{
    public partial class KaoQinList : BaseUserControl
    {
        protected string BrowseKQResultNolimited = "";
        protected string DeptPathNolimited = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblErrorMessage.ForeColor = System.Drawing.Color.Red;
            this.lblErrorMessage.Text = "";
            this.txtOtherUser.Visible = false;

            this.DataManager.Close();
            this.DataManager.Open(System.Configuration.ConfigurationManager.AppSettings.Get("KQConnectionString"));
            this.dgList.DataManager = DataManager;

            if (System.Configuration.ConfigurationManager.AppSettings.Get("BrowseKQResultNolimited") != null)
            {
                BrowseKQResultNolimited = System.Configuration.ConfigurationManager.AppSettings.Get("BrowseKQResultNolimited");
                BrowseKQResultNolimited = BrowseKQResultNolimited.ToLower();
                BrowseKQResultNolimited = BrowseKQResultNolimited.Replace(" ", "");
            }

            if (!this.IsPostBack)
            {
                if (DateTime.Now.Day < 7)
                {
                    DateTime tmp = DateTime.Now.AddMonths(-1);
                    this.txtStartDate.Text = tmp.ToString("yyyy") + "-" + tmp.ToString("MM") + "-" + "01";
                }
                else
                {
                    this.txtStartDate.Text = DateTime.Now.ToString("yyyy") + "-" + DateTime.Now.ToString("MM") + "-" + "01";
                }
                this.txtEndDate.Text = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
                this.txtOtherUser.Text = this.LogonUser.UserID.Trim();
                this.ShowButton1();
                this.BindLstType();
                this.BindLstUser();
                this.BindList();
            }
            
        }

        private void BindList()
        {
            try
            {
                string strUserAccount = "";
                string LogonUser = "";
                string SearchEmployeeName = "";
                string SearchEmployeeAccount = "";

                LogonUser = this.LogonUser.UserID.ToLower().Trim().Replace("centaline\\", "");

                if (this.txtOtherUserForInput.Text != "")
                {
                    if (BrowseKQResultNolimited.IndexOf(LogonUser) >= 0)
                    {
                        this.txtOtherUser.Text = this.txtOtherUserForInput.Text.Trim();
                    }
                    else
                    {
                        if (this.txtOtherUser.Text != "")
                        {
                            SearchEmployeeName = this.txtOtherUserForInput.Text.Trim();
                            SearchEmployeeAccount = this.txtOtherUserForInput.Text.Trim();
                            SearchEmployeeAccount = SearchEmployeeAccount.Replace("centaline\\", "");
                            SearchEmployeeAccount = "centaline\\" + SearchEmployeeAccount;
                        }

                        BusinessEntiry employee = new BusinessEntiry(this.DataManager, "HRUser", "RowGUID");
                        if (employee.Open("DomainAccount = '" + SearchEmployeeAccount + "' or DomainAccount = '" + SearchEmployeeName + "' or name='" + SearchEmployeeName + "'"))
                        {

                            SHZYK3.K3 pobj = new SHZYK3.K3();

                            if (pobj.IsSubOrdinate(this.LogonUser.UserID.Trim(), employee.GetPropertyValue("DomainAccount")))
                            {
                                this.txtOtherUser.Text = this.txtOtherUserForInput.Text;
                            }
                            else
                            {
                                this.txtOtherUser.Text = "unknow employee";
                                this.lblErrorMessage.Text = "[" + SearchEmployeeName + "]不是你的下级，无权查看";
                            }
                        }
                    }
                }

                string strFilter = " And basicTime Is Not Null ";
                if (this.txtStartDate.Text != "")
                {
                    strFilter += String.Format(@" And AttendanceDate >='{0}'", this.txtStartDate.Text.Trim());
                }
                if (this.txtEndDate.Text != "")
                {
                    strFilter += String.Format(@" And AttendanceDate <='{0}'", this.txtEndDate.Text.Trim());
                }
                if (this.txtOtherUser.Text != "")
                {
                    if (this.txtOtherUserForInput.Text != "")
                    {
                        strUserAccount = this.txtOtherUser.Text.Trim();
                        strUserAccount = strUserAccount.ToLower();
                        strUserAccount = strUserAccount.Replace("centaline\\", "");

                        strFilter += String.Format(@" And (DomainAccount = 'centaline\{0}' Or UserName = '{0}')", strUserAccount);
                    }
                    else
                    {
                        strFilter += String.Format(@" And UserRowGUID = '{0}'", this.lstUser.SelectedValue);
                    }                    
                }
                else
                {
                    strFilter += String.Format(@" And UserRowGUID = '{0}'", this.lstUser.SelectedValue);
                }
                if (this.lstType.SelectedValue != "")
                {
                    strFilter += string.Format(@" And Exception_Identify = '{0}'", this.lstType.SelectedValue);
                }


                strFilter += " Order By AttendanceDate Desc,BasicTime Desc";

                this.dgList.DataSourceTable = "ViewT_CProcessData";
                this.dgList.DataSourceFilter = strFilter; 
                this.dgList.BindData();
            }
            catch (Exception ex)
            {
                //throw ex;
                this.lblErrorMessage.Text = ex.Message;
            }
        }

        protected void dgList_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            string url = "";

            if (e.Item.ItemIndex >= 0)
            {
                if (e.Item.Cells[2].Text != "正常")
                {
                    //url += "?basicTime=" + e.Item.Cells[0].Text + "&ApplyTypeCode=BQK";
                    url += "../../pages/WorkFlow/KQ/KQApply.aspx";
                    e.Item.Cells[2].Attributes.Add("onclick", "return ShowDialog('" + url + "', 650, 450, '');");
                    e.Item.Cells[2].Text = "<a href=# style='text-decoration:none'>" + e.Item.Cells[2].Text + "</a>";
                }

                if (e.Item.Cells[e.Item.Cells.Count - 1].Text.Trim() != "" && e.Item.Cells[e.Item.Cells.Count - 1].Text != "&nbsp;")
                {
                    e.Item.Cells[2].Attributes["title"] = e.Item.Cells[e.Item.Cells.Count - 1].Text;
                }
            }
        }

        public void BindLstUser()
        {
            System.Data.SqlClient.SqlDataReader rs = null;
            string sql = "";
            string UserID = "{9C7D9CB4-A70A-4A82-B97B-A32521298A4A}";
            try
            {
                BusinessEntiry user = new BusinessEntiry(this.DataManager, "HRUser", "RowGUID");
                user.Open("DomainAccount = 'centaline\\" + this.LogonUser.UserID.ToLower().Trim() + "'");

                if (user.GetPropertyValue("RowGUID").Trim() == "")
                {
                    sql = "SELECT '" + UserID + "' as RowGUID,'未授权用户' as name ";
                }
                else
                {
                    sql = "SELECT RowGUID,name from HRUser where RowGUID in ('" + user.GetPropertyValue("RowGUID") + "')";
                    UserID = user.GetPropertyValue("RowGUID");
                }

                this.DataManager.BindWebDropDownList(this.lstUser, "name", "RowGUID", sql);
                this.lstUser.SelectedValue = UserID;

                //this.Button1.Visible = true;
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                if (rs != null && !rs.IsClosed)
                {
                    rs.Close();
                }
            }

        }

        public void BindLstType()
        {
            string sql = "";
            try 
            {
                sql = "Select Exception_Identify,Exception_Name From T_CAllExceptionType (nolock)";
                this.DataManager.BindWebDropDownList(this.lstType, "Exception_Name", "Exception_Identify", sql, "全部");
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        public void ShowButton1()
        {
            this.Button1.Visible = false;
            DeptPathNolimited = System.Configuration.ConfigurationManager.AppSettings.Get("DeptPathNolimited");

            string[] arDeptPathNolimited = DeptPathNolimited.Split(';');
            foreach (string deptPathNolimited in arDeptPathNolimited)
            {
                if (this.LogonUser.DeptPath != null)
                {
                    if (this.LogonUser.DeptPath.IndexOf(deptPathNolimited) >= 0)
                    {
                        this.Button1.Visible = true;
                        return;
                    }
                }
                else
                {
                    return;
                }
            }             
        }
        
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            this.BindList();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string SqlTxt = "";
            string UserAccount = "";
            string CardTime = "";
            string DispInfo = "";

            Com.DAO.DataManager pobj = new DataManager();
            
            try
            {
                CardTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                UserAccount = "centaline\\" + this.LogonUser.UserID.Trim();
                SqlTxt = "exec  [dbo].[HandCardTime] '" + UserAccount + "','" + CardTime + "'";
                pobj.Open(System.Configuration.ConfigurationManager.AppSettings.Get("KQConnectionString"));
                pobj.Execute(SqlTxt);

                CardTime = "您的签卡时间为:" + CardTime;

            }
            catch (Exception ex)
            {
                CardTime = "签卡错误:" + ex.Message;
                CardTime = CardTime.Replace("\r", "");
                CardTime = CardTime.Replace("\n", "");
            }
            finally
            {
                try
                {
                    if (pobj != null && !pobj.IsClosed)
                    {
                        pobj.CloseConnection();
                    }
                }
                catch (Exception)
                {
                }
                CardTime = CardTime.Replace("'", "");
                DispInfo = "";
                DispInfo = DispInfo + "<script language='javascript'> \r\n";
                DispInfo = DispInfo + "		alert(\"" + CardTime + "\");\r\n ";
                DispInfo = DispInfo + "</script>\r\n";
                //Response.Write(DispInfo);
                this.ShowMessage(CardTime, false);
            }
        }

    }
}